package com.cyz.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.admin.service.StudyResourceTypeIdService;
import com.cyz.common.bean.StudyResource;
import com.cyz.common.bean.StudyResourceTypeId;
import com.cyz.common.dto.StudyResourceDto;
import com.cyz.common.enums.MyEnums;
import com.cyz.common.mapper.StudyResourceMapper;
import com.cyz.admin.service.StudyResourceService;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.common.utils.R;
import com.cyz.admin.vo.StudyResourceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author 34928
* @description 针对表【study_resource】的数据库操作Service实现
* @createDate 2023-02-22 14:32:26
*/
@Service
public class StudyResourceServiceImpl extends ServiceImpl<StudyResourceMapper, StudyResource>
    implements StudyResourceService {

    @Autowired
    private StudyResourceTypeIdService studyResourceTypeIdService;

    @Override
    public R queryStudyResources() {
        //查询数据
        List<StudyResource> list = list();
        //错误判断
        if(list.isEmpty()){
            return R.error(MyEnums.QUERY_FAIL);
        }
        return R.success(list);
    }

    @Override
    public R deleteStudyResources(Integer[] ids) {
        //删除数据
        boolean flag = removeByIds(Arrays.asList(ids));
        //错误判断
        if(flag){
            return queryStudyResourcesAndTypes();
        }
        return R.error(MyEnums.DELETE_FAIL);
    }

    @Override
    public R addStudyResource(StudyResourceDto studyResourceDto) {
        //dto转换
        StudyResource studyResource = BeanCopyUtil.copyBean(studyResourceDto, StudyResource.class);
        //增加数据
        boolean flag = save(studyResource);
        studyResourceDto.getTypeIds().forEach(item ->{
            studyResourceTypeIdService.save(new StudyResourceTypeId(studyResource.getId(),item));
        });

        //错误判断
        if(flag){
            return queryStudyResourcesAndTypes();
        }
        return R.error(MyEnums.ADD_FAIL);
    }

    @Override
    public R updateStudyResource(StudyResourceDto studyResourceDto) {
        //dto转换
        StudyResource studyResource = BeanCopyUtil.copyBean(studyResourceDto, StudyResource.class);
        //更新数据
        boolean flag = updateById(studyResource);
        studyResourceTypeIdService.removeById(studyResource.getId());
        studyResourceDto.getTypeIds().forEach(item ->{
            studyResourceTypeIdService.save(new StudyResourceTypeId(studyResource.getId(),item));
        });
        //错误判断
        if(flag){
            return queryStudyResourcesAndTypes();
        }
        return R.error(MyEnums.UPDATE_FAIL);
    }

    @Override
    public R queryStudyResourcesAndTypes() {
        //查询数据
        List<StudyResource> list = list();
        List<StudyResourceVo> studyResourceVos = BeanCopyUtil.copyBeanList(list, StudyResourceVo.class);
        studyResourceVos.forEach(item->{
            item.setTypeNames(studyResourceTypeIdService.getTypeNamesByStudyResourceId(item.getId()));
            item.setTypeIds(studyResourceTypeIdService.getTypeIdsByStudyResourceId(item.getId()));
        });
        //错误判断
        if(list.isEmpty()){
            return R.error(MyEnums.QUERY_FAIL);
        }
        return R.success(studyResourceVos);
    }
}




